package cn.csc.module.product.controller.admin.tagref;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.pojo.PageResult;
import cn.csc.framework.excel.core.util.ExcelUtils;
import cn.csc.framework.operatelog.core.annotations.OperateLog;
import cn.csc.module.product.controller.admin.tagref.vo.*;
import cn.csc.module.product.convert.tagref.SpuTagReferenceConvert;
import cn.csc.module.product.dal.dataobject.tagref.SpuTagReferenceDO;
import cn.csc.module.product.service.tagref.SpuTagReferenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.pojo.CommonResult.success;
import static cn.csc.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 商品标签关联信息")
@RestController
@RequestMapping("/product/spu-tag-reference")
@Validated
public class SpuTagReferenceController {

    @Resource
    private SpuTagReferenceService spuTagReferenceService;

    @PostMapping("/create")
    @ApiOperation("创建商品标签关联信息")
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:create')")
    public CommonResult<BigInteger> createSpuTagReference(@Valid @RequestBody SpuTagReferenceCreateReqVO createReqVO) {
        return success(spuTagReferenceService.createSpuTagReference(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新商品标签关联信息")
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:update')")
    public CommonResult<Boolean> updateSpuTagReference(@Valid @RequestBody SpuTagReferenceUpdateReqVO updateReqVO) {
        spuTagReferenceService.updateSpuTagReference(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除商品标签关联信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:delete')")
    public CommonResult<Boolean> deleteSpuTagReference(@RequestParam("id") BigInteger id) {
        spuTagReferenceService.deleteSpuTagReference(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得商品标签关联信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = BigInteger.class)
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:query')")
    public CommonResult<SpuTagReferenceRespVO> getSpuTagReference(@RequestParam("id") BigInteger id) {
        SpuTagReferenceDO spuTagReference = spuTagReferenceService.getSpuTagReference(id);
        return success(SpuTagReferenceConvert.INSTANCE.convert(spuTagReference));
    }

    @GetMapping("/list")
    @ApiOperation("获得商品标签关联信息列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:query')")
    public CommonResult<List<SpuTagReferenceRespVO>> getSpuTagReferenceList(@RequestParam("ids") Collection<BigInteger> ids) {
        List<SpuTagReferenceDO> list = spuTagReferenceService.getSpuTagReferenceList(ids);
        return success(SpuTagReferenceConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得商品标签关联信息分页")
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:query')")
    public CommonResult<PageResult<SpuTagReferenceRespVO>> getSpuTagReferencePage(@Valid SpuTagReferencePageReqVO pageVO) {
        PageResult<SpuTagReferenceDO> pageResult = spuTagReferenceService.getSpuTagReferencePage(pageVO);
        return success(SpuTagReferenceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出商品标签关联信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:spu-tag-reference:export')")
    @OperateLog(type = EXPORT)
    public void exportSpuTagReferenceExcel(@Valid SpuTagReferenceExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SpuTagReferenceDO> list = spuTagReferenceService.getSpuTagReferenceList(exportReqVO);
        // 导出 Excel
        List<SpuTagReferenceExcelVO> datas = SpuTagReferenceConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "商品标签关联信息.xls", "数据", SpuTagReferenceExcelVO.class, datas);
    }

}
